package com.eboss.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ScrollView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.adapter.BarcodeMonthListAdapter;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.BarcodeMonthEntity;
import com.eboss.sbs.model.BarcodeMonthListEntity;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mytools.tool.views.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BarcodeMonthActivity extends BaseActivity implements OnItemClickListener {

	public static String BARCODE_ID = "BARCODE_ID";
	private int barcodeId;

	private PullToRefreshScrollView scroolView;
	
	private MyListView listview;
	private BarcodeMonthListAdapter adapter;
	private List<BarcodeMonthEntity> datas;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_barcode_month);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "二维码详情");
		
		barcodeId = getIntent().getIntExtra(BARCODE_ID, -1);
		
		scroolView = (PullToRefreshScrollView) findViewById(R.id.scroolView);
		scroolView.setMode(Mode.PULL_DOWN_TO_REFRESH);
		scroolView.setOnRefreshListener(new PullToRefreshViewEvent()); //下拉刷新 上拉加载
		
		datas = new ArrayList<BarcodeMonthEntity>();
		listview = (MyListView)findViewById(R.id.listview);
		listview.setOnItemClickListener(this);
		adapter = new BarcodeMonthListAdapter(this, datas);
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		
		listview.setAdapter(adapter);
		
		getdata();
	}
	
	//下拉刷新 上拉加载
	public class PullToRefreshViewEvent implements OnRefreshListener<ScrollView>{
		@Override
		public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
			getdata();
		}
	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("barcodeId", barcodeId);
		API.http_post(this, API.BARCODE_MONTH, params, new HttpCallback<BarcodeMonthListEntity>(this) {
			@Override
			public void onSuccess(int statusCode, BarcodeMonthListEntity data) {
				scroolView.onRefreshComplete();
				if(data!=null){
					setTvText(R.id.tv_barcode_name, data.getBarcodeName());
					setTvText(R.id.tv_applicant, data.getApplicant());
					setTvText(R.id.tv_contact_phone, data.getPhone());
					setTvText(R.id.tv_time, data.getTime());
					setTvText(R.id.tv_contributions, data.getContributions()+"人");
					datas.clear();
					List<BarcodeMonthEntity> datas_tmp = data.getMonthdatas();
					if(datas_tmp!=null&datas_tmp.size()>0){
						datas.addAll(datas_tmp);
					}
					adapter.notifyDataSetChanged();
				}
			}

			@Override
			public void onFailure(int statusCode, String message) {
				scroolView.onRefreshComplete();
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<BarcodeMonthListEntity>() {};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
	
			default:break;
		}
		super.onClick(v);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent();
		intent.setClass(mActivity, BarcodeDayActivity.class);
		intent.putExtra(BarcodeDayActivity.BARCODE_ID, datas.get(position).getMonthStr());
		intent.putExtra(BarcodeDayActivity.MONTH_STR, datas.get(position).getMonthStr());
		startActivity(intent);
	}
	
}
